<template>
  <div class="hy-table">
    <div class="header">
      <slot name="header">
        <div class="title">
          <slot name="headerHandlerLeft"></slot>
          <!-- {{ title }} -->
        </div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        align="center"
        width="80"
      ></el-table-column>
      <el-table-column
        v-for="propItem in propList"
        :key="propItem.prop"
        v-bind="propItem"
        align="center"
        show-overflow-tooltip
      >
        <template #default="scope">
          <slot :name="propItem.slotName" :row="scope.row">
            {{ scope.row[propItem.prop] }}
          </slot>
        </template>
      </el-table-column>
    </el-table>

    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'table-vue2',
  model: {
    prop: 'page',
    event: 'update:page'
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    listData: {
      type: Array,
      required: true
    },
    propList: {
      type: Array,
      required: true
    },
    showIndexColumn: {
      // 是否显示序号
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      // 是否要选择☑
      type: Boolean,
      default: false
    },
    showFooter: {
      // 是否分页
      type: Boolean,
      default: true
    },
    listCount: {
      // 分页页码
      type: Number,
      dufault: 0
    },
    page: {
      type: Object,
      defualt: () => ({ currentPage: 0, pageSize: 10 })
    },
    childrenProps: {
      type: Object,
      defaule: () => ({})
    }
  },
  methods: {
    handleSelectionChange(value) {
      // 是否要选择☑传值
      console.log(value)
      this.$emit('selectionChange', value)
    },
    handleCurrentChange(currentPage) {
      console.log(currentPage, '11')
      this.$emit('update:page', { ...this.page, currentPage })
    },
    handleSizeChange(pageSize) {
      this.$emit('update:page', { ...this.page, pageSize })
    }
  },
  created() {
    // console.log(this.page)
  }
}
</script>

<style scoped lang="scss">
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  margin-top: 15px;

  .el-pagination {
    text-align: right;
  }
}
</style>
